﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStandardItemModel>
#include <QDebug>
#include <QDateTime>
#include <QComboBox>
#include "delegate.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->initTable(ui->tableView,27,true);
    this->init();
    setWindowTitle("Delegate 作者:m晴朗 博客主页:https://blog.csdn.net/qq_40666149");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    QStringList columnNames;
    QList<int> columnWidths;
    columnNames<<"chbox"<<"pix"<<"lineEdit"<<"sbox"<<"cbox"<<"QDateTimeEdit"<<
                 "QDateEdit"<<"QTimeEdit"<<"scbox"<<"button"<<"pgbar";
    columnWidths<<30<<35<<120<<50<<130<<150<<100<<80<<115<<150<<115;

    model = new QStandardItemModel;
    model->setHorizontalHeaderLabels(columnNames);
    ui->tableView->setModel(model);
    int count = columnNames.count();
    for(int i = 0;i<count;i++)
    {
        ui->tableView->setColumnWidth(i,columnWidths.at(i));
    }
    this->initDelegate();
}

void MainWindow::initTable(QTableView *tableView,int rowHeight,bool Editable,bool isSorting,bool verticalHeadVisible,bool isLastTensile,bool isShowGrid)
{
    /*设置默认行高*/
    tableView->verticalHeader()->setDefaultSectionSize(rowHeight);
    /*设置交替行颜色--允许交替行颜色*/
    tableView->setAlternatingRowColors(true);
    /*设置水平/垂直滚动模式--一次滚动一个项目*/
    tableView->setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
    tableView->setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
    /*设置选择行为--每次选择只有一整行*/
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    /*设置拖放行为--不允许拖放*/
    tableView->setDragDropMode(QAbstractItemView::NoDragDrop);
    /*设置选择模式--只能选择一个项目*/
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    /*设置Tab导航键--允许使用Tab键导航，shift+tab反向导航*/
    tableView->setTabKeyNavigation(true);
    /*设置是否自动换行--取消自动换行*/
    tableView->setWordWrap(false);
    /*设置文本省略模式--省略号不会出现在文本中*/
    tableView->setTextElideMode(Qt::ElideNone);
    /*设置左上角全选按钮--禁用*/
    tableView->setCornerButtonEnabled(false);
    /*设置是否支持表头排序--应该和表头是否可以单击保持一致*/
    tableView->setSortingEnabled(isSorting);
    /*设置是否显示网格线*/
    tableView->setShowGrid(isShowGrid);
    /*设置垂直表头是否可见*/
    tableView->verticalHeader()->setVisible(verticalHeadVisible);
    /*设置选中一行表头是否加粗--不加粗*/
    tableView->horizontalHeader()->setHighlightSections(false);
    /*设置最后一行是否拉伸填充*/
    tableView->horizontalHeader()->setStretchLastSection(isLastTensile);
    /*设置行标题最小宽度尺寸*/
    tableView->horizontalHeader()->setMinimumSectionSize(0);
    /*设置行标题最小高度*/
    tableView->horizontalHeader()->setFixedHeight(rowHeight);
    /*设置表头是否可以单击--不可单击*/
    tableView->horizontalHeader()->setSectionsClickable(isSorting);

    /*是否可编辑*/
    if(Editable)
    {
        tableView->setEditTriggers(QAbstractItemView::CurrentChanged|QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
    }
    else
    {
        tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

void MainWindow::initDelegate()
{
    cboxitem<<"PM1"<<"PM2"<<"PM3"<<"PM4"<<"PM5";

    Delegate *checkDelegate = new Delegate;
    checkDelegate->setDelegateType("QCheckBox");
    checkDelegate->setDelegateColumn(0);
    ui->tableView->setItemDelegateForColumn(0,checkDelegate);

    Delegate *pixDelegate = new Delegate;
    pixDelegate->setDelegateType("QPixmap");
    pixDelegate->setDelegateColumn(1);
    pixDelegate->setJudgmentColumn(3);
    pixDelegate->setJudgmentCondition(">");
    pixDelegate->setJudgmentValue("10");
    pixDelegate->setImgNo(QPixmap(":/erro.png"));
    pixDelegate->setImgOk(QPixmap(":/corr.png"));
    ui->tableView->setItemDelegateForColumn(1,pixDelegate);

    Delegate *lineEidtDelegate = new Delegate;
    lineEidtDelegate->setDelegateType("QLineEdit");
    lineEidtDelegate->setDelegateColumn(2);
    lineEidtDelegate->setDelegatePwd(true);
    ui->tableView->setItemDelegateForColumn(2,lineEidtDelegate);
    connect(lineEidtDelegate,SIGNAL(valueChanged(QString)),this,SLOT(showData(QString)));

    Delegate * sboxDelegate = new Delegate;
    sboxDelegate->setDelegateType("QSpinBox");
    ui->tableView->setItemDelegateForColumn(3,sboxDelegate);
    connect(sboxDelegate,SIGNAL(valueChanged(QString)),this,SLOT(setNum(QString)));


    Delegate *cboxDelegate = new Delegate;
    cboxDelegate->setDelegateType("QComboBox");
    cboxDelegate->setCboxItems(cboxitem);
    cboxDelegate->setCboxEditable(false);
    ui->tableView->setItemDelegateForColumn(4,cboxDelegate);

    Delegate *dateTimeEditDelegate = new Delegate;
    dateTimeEditDelegate->setDelegateType("QDateTimeEdit");
    ui->tableView->setItemDelegateForColumn(5,dateTimeEditDelegate);

    Delegate *dateEditDelegate = new Delegate;
    dateEditDelegate->setDelegateType("QDateEdit");
    ui->tableView->setItemDelegateForColumn(6,dateEditDelegate);

    Delegate *timeEditDelegate = new Delegate;
    timeEditDelegate->setDelegateType("QTimeEdit");
    ui->tableView->setItemDelegateForColumn(7,timeEditDelegate);

    Delegate *btDelegate = new Delegate;
    btDelegate->setDelegateType("QPushButton");
    btDelegate->setDelegateColumn(9);
    ui->tableView->setItemDelegateForColumn(9,btDelegate);
    connect(btDelegate,SIGNAL(buttonClicked(int,QModelIndex)),this,SLOT(buttonClicked(int,QModelIndex)));

    Delegate *pgbarDelegate = new Delegate;
    pgbarDelegate->setDelegateType("QProgressBar");
    pgbarDelegate->setDelegateColumn(10);
    ui->tableView->setItemDelegateForColumn(10,pgbarDelegate);

    initData();

    //此方式不是委托
    Delegate *scboxDelegate = new Delegate;
    scboxDelegate->setCboxItems(cboxitem);
    scboxDelegate->setDelegateColumn(8);
    scboxDelegate->setComboBox(ui->tableView,model);
}

void MainWindow::setNum(QString value)
{
    QModelIndex currentIndex = ui->tableView->currentIndex();

    if(currentIndex.column() == 3)
    {
        model->setData(model->index(currentIndex.row(),10),value.toInt());
    }
}

void MainWindow::buttonClicked(int btnIndex, const QModelIndex &index)
{
    if(btnIndex == 0)
    {
        model->setData(model->index(index.row(),5),20);
    }
    else if(btnIndex == 1)
    {
        model->setData(model->index(index.row(),5),40);
    }
    else if(btnIndex == 2)
    {
        model->setData(model->index(index.row(),5),60);
    }

}

void MainWindow::showData(QString str)
{
    qDebug()<<str;
}

void MainWindow::initData()
{
    QString dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
    QString date = QDateTime::currentDateTime().toString("yyyy-MM-dd");
    QString time = QDateTime::currentDateTime().toString("HH:mm:ss");
    QList<QStandardItem *> items;
    for(int row=0;row<10;row++)
    {
        items<< new QStandardItem("true")<< new QStandardItem("") << new QStandardItem("123456") << new QStandardItem("10")
             << new QStandardItem(cboxitem.at(0)) << new QStandardItem(dateTime)  << new QStandardItem(date)
             << new QStandardItem(time) << new QStandardItem()<< new QStandardItem("open|close")
             << new QStandardItem("10");

        model->insertRow(row,items);
        items.clear();
    }//end

    model->setData(model->index(0,0),"false");
    model->setData(model->index(3,0),"false");
    model->setData(model->index(5,0),"false");

    model->setData(model->index(3,3),"20");
    model->setData(model->index(5,3),"40");

    model->setData(model->index(3,10),"20");
    model->setData(model->index(5,10),"40");
}

void MainWindow::on_getDataBt_clicked()
{
    QFont font = ui->textEdit->font();
    font.setPointSize(11);
    ui->textEdit->setFont(font);

    ui->textEdit->clear();
    QString rowStr = "";
    for (int row = 0; row < model->rowCount(); ++row)
    {
        for (int column = 0; column < model->columnCount(); ++column)
        {
            if(column == 8)
            {
                QComboBox *cbox = (QComboBox*)ui->tableView->indexWidget(model->index(row,column));
                QString str = cbox->currentText();
                rowStr += str+" ";
            }
            QModelIndex index = model->index(row, column);
            QString data = model->data(index).toString();
            rowStr += data + " ";
        }
        ui->textEdit->append(rowStr);
        ui->textEdit->append("");
        rowStr.clear();
    }
}
